package com.lavegue.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.lavegue.common.BaseHibernateDao;
import com.lavegue.common.Page;
import com.lavegue.dao.BeadrollDao;
import com.lavegue.model.Beadroll;
import com.lavegue.model.UserInfo;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class BeadrollDaoImpl extends BaseHibernateDao<Beadroll, Integer> implements BeadrollDao{

	//默认构造函数
	public BeadrollDaoImpl() {
		super(Beadroll.class);
	}
	/**
	 * 删除名录记录
	 * @param bid
	 * @return
	 */
	public boolean deleteBeadroll(Beadroll beadroll) {
		try {
			this.getHibernateTemplate().delete(beadroll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
			
		return true;
	}

	/**20120229
	 * 查询名录表
	 * @param bid
	 * @return
	 */
	public Beadroll queryBeadroll(Integer bid) {
		String sqlStr="from Beadroll where id=?";
		List<Beadroll> beadrolls =  this.getHibernateTemplate().find(sqlStr,bid);
		if(null != beadrolls){
			return (Beadroll) beadrolls.get(0);
		}
		return null;
	}
	/**
	 * 修改名录记录
	 * @param beadroll
	 * @return
	 */
	public boolean updateBeadroll(Beadroll beadroll) {
		try {
			this.getHibernateTemplate().update(beadroll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	/**
	 * 新增名录记录
	 * @param beadroll
	 * @return
	 */
	public boolean insertBeadroll(Beadroll beadroll){
		try {
			this.getHibernateTemplate().save(beadroll);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	/** 查询名录数据列表
	 * 20120229
	 * @param pageNo页数
	 * @param pageSize2     一页显示的行数
	 * @param provinceId 省份ID
	 * @param cityId 城市ID
	 * @return
	 */
	public Page<Beadroll> findAllBeadrollList(int pageNo,int pageSize2,Integer provinceId,Integer cityId){
		Page<Beadroll> page = null;
		List<Criterion> conditions = new ArrayList<Criterion>();
		Order order = Order.desc("id"); //降序
		if(null != provinceId){
			conditions.add(Restrictions.eq("provinceArea.id", provinceId));
			if(null != cityId){
				conditions.add(Restrictions.eq("cityArea.id", cityId));
			}
		}
		
		try {
			//查询名录数据列表
			page = this.findPage(pageNo, pageSize2, conditions, order);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return page;
	}
	
	/**前台-随机查询名录数据列表100条数据
	 * 20120305  搜索100条数据（随机100条数据）
	 * @param pageNo页数
	 * @param pageSize2     一页显示的条数
	 * @param provinceId 省份ID
	 * @param cityId 城市ID
	 * @return
	 */
	public Page<Beadroll> findRandomAllBeadrollList(int pageNo,int pageSize2,Integer provinceId,Integer cityId){
		Page<Beadroll> page = null;
		List<Beadroll> data = new ArrayList<Beadroll>();
		int cnt = 0;
		Random random = new Random();
		List<Integer> ids = getBeadrollIds(provinceId, cityId);
		int ps = pageSize2; //每页的显示的条数20
		if (ids.size() < ps) {
			ps = ids.size();
		}
		while (cnt < ps) { //循环只要20条随机数据
			int id = ids.get(random.nextInt(ids.size()));
			Beadroll o = this.findById(id); //通过ID查询
			if (!data.contains(o)) {	//比较list里的数据是否有重复的
				data.add(o);
				cnt++;
			}
		}
		page = new Page<Beadroll>(pageNo, ids.size(), data, pageSize2);
		if (page.getTotalRecord() > 100) {
			page.setTotalRecord(100);
		}
		if (page.getTotalPage() >5) {
			page.setTotalPage(5);
		}
		if(pageNo == 5) {
			page.setHasNextPage(false);
		}
		return page;
	}
	
	/**
	 * 查询所有名录的ID
	 * @param province
	 * @param city
	 * @return
	 */
	private List<Integer> getBeadrollIds(int province, int city) {
		return this.getHibernateTemplate().find("select id from Beadroll where provinceArea.id=? and cityArea.id=?", new Object[]{province, city});
	}

}
